<template>
    <div class="container" id="vue-container">
        <div class="top">
            <div class="header">
                <img src="../assets/img/search-icon.png">
<!--                输入书名-->
                <form @submit="search_title($event)">
                    <input id="input" v-model="search.title"  autofocus="autofocus" type="search" placeholder="搜索书名">
                </form>
                <!--<div class="lishi">
                    <div>历史记录</div>
                </div>-->
            </div>
            <div class="quxiao" @click="search_title($event)">搜索</div>
        </div>
<!--        显示搜索出的图书-->
        <div class="shuji" v-if="shuji">
            <div class="intro" v-for="book in books" :key="book.ID">
                <router-link :to="{path:'/book_details',query:{id:book.ID}}" >
                    <div class="intro_tu">
                        <img v-lazy="book.ImageUrl">
                    </div>
                    <div class="intro_right">
                        <p class="intro_title">{{book.Title}}</p>
                        <p class="intro_intro">{{book.Description}}</p>
                        <span class="intro_fenlei">{{book.TagName}}</span>
                    </div>
                </router-link>
            </div>
            <div class="tishi">{{tishi}}</div>
        </div>
<!--        下面的推荐-->
        <div v-else>
            <div class="remen">
                <div class="tit">热门搜索</div>
                <div class="shuname">
                    <router-link v-for="hot_book in hot_books" :key="hot_book.ID" :to="{path:'/book_details',query:{id:hot_book.ID}}" tag="div">{{hot_book.Title}}</router-link>
                </div>
            </div>
            <div class="head">
                <div class="title">畅销书单</div>
                <div class="content">
                    <div v-for="sellers_book in sellers_books" :key="sellers_book.ID">
                        <router-link :to="{path:'/book_details',query:{id:sellers_book.ID}}"><img v-lazy=sellers_book.ImageUrl></router-link>
                        <p>{{sellers_book.Title}}</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "search",
        data(){
            return{
                search: {
                    title:"",
                    tag_id: 0, // 标签 ID
                },
                tags: [], // 所有的分类列表
                books: [], // 显示的图书列表
                hot_books:[], //热门搜索列表
                sellers_books:[], //畅销榜单列表
                tishi:'',
                shuji:false,
            }
        },
        created: function() {
            this.set()
            this.book_hot()
            this.best_sellers()
        },
        methods: {
            set:function(){
                this.$emit('set','hide')
            },
            //热门搜索
            book_hot:function(){
                this.$axios.get('/books',{params: {PtID:0,ForbidView:2,Finished:0,Sex:0,Limit:6,Offset:0,Order:'UserRec',Descending:false}}).then(response=>{
                    this.hot_books=response.data.books
                })
            },
            //畅销榜单
            best_sellers:function(){
                this.$axios.get('/books',{params: {PtID:0,ForbidView:2,Finished:0,Sex:0,Limit:8,Offset:0,Order:'AdminRec',Descending:false}}).then(response=>{
                    this.sellers_books=response.data.books
                })
            },
            //输入框搜索
            search_title: function(evt) {
                evt.preventDefault();
                if(this.search.title) {
                    this.$axios.get('/search-books',{params:{AuthorTitle:this.search.title}}).then(response=>{
                        this.shuji=true
                        if(response.data.books.length!=0){
                            this.books = response.data.books;
                            this.tishi=''
                        }else{
                            this.books = []
                            this.tishi="没有搜索到，换个关键字吧"
                        }
                    })
                } else {
                    alert("请输入关键字")
                }
            },
        }
    }
</script>

<style scoped>
    .container {
        background: white;
        margin-bottom: 20px;
        padding: 0px;
    }
    .header {
        display: flex;
        flex: 1;
        flex-direction: row;
        z-index: 100;
        overflow: hidden;
        background: #eeeeee;
        border-radius:5px ;
        position: relative;
        margin-left: 10px;
    }
    .header img {
        width: 2rem;
        height: 2rem;
        border: 0;
    }
    form{
        display: flex;
        align-items: center;
        width: 85%;
    }
    .header input {
        border: none;
        outline: none;
        margin-left: 5px;
        -webkit-appearance: none;
        background: none;
        color: black;
        width: 100%;
    }
    .top{
        display: flex;
        position: fixed;
        top: 0px;
        width: 100%;
        background: white;
        align-items: center;
        padding: 10px 0px;
        border-bottom: 1px solid #DDDDDD;
    }
    .quxiao{
        margin: 0px 15px;
        font-size: 0.95rem;
        color: #3688FF;
    }
    .content div{
        width: 22%;
    }
    .content img {
        width: 100%;
        height: 15vh;
        -webkit-box-shadow: 0 4px 10px rgba(0,0,0,.2);
        box-shadow: 0 4px 10px rgba(0,0,0,.2);
    }

    .content p {
        width: 100%;
        margin-top: 10px;
        font-size: 0.85rem;
    }

    .content {
        padding: 10px 15px;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: space-between;
    }

    .intro a {
        margin-top: 20px;
        display: flex;
        text-decoration: none;
        color: black;
    }

    .intro img {
        width: 6.5rem;
        height: 8.7rem;
        -webkit-box-shadow: 0 4px 10px rgba(0,0,0,.2);
        box-shadow: 0 4px 10px rgba(0,0,0,.2);
    }
    .intro_tu{
        margin-left: 15px;
    }
    .intro_right {
        margin: 0px 15px;
    }

    .intro_title {
        font-size: 1.2rem;
        margin: 0px;
    }

    .intro_writer {
        color: #c1c1c1;
        font-size: 10px;
    }

    .intro_intro {
        margin: 13px 0px;
        color: #999;
        font-size: 1rem;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .intro_fenlei {
        text-align: center;
        font-size: 1rem;
        color: #989898;
    }
    .head{

    }
    .title{
        color: #3688FF;
        padding: 10px 0px;
        padding-left: 15px;
        font-size: 1.2rem;
    }
    .shuji{
        margin-top: 80px;
        padding-bottom: 20px;
    }
    .tishi{
        text-align: center;
    }
    .remen{
        margin-top: 60px;
        padding: 15px;
    }
    .tit{
        font-size: 1.2rem;
        color: #3688FF;
    }
    .shuname{
        display: flex;
        flex-wrap: wrap;
        margin-top: 10px;
    }
    .shuname div{
        border: 1px solid #C1C1C1;
        border-radius:20px ;
        padding: 0px 8px;
        margin: 10px 10px 0px 0px;
        color:#999;
        font-size: 0.9rem;
    }

</style>
